<template>
  <div class="seller-container">
    <div class="title" :style="comStyle">
      <span>| {{ showTitle }}</span>
      <span :style="comStyle" class="iconfont" @click="showChoice = !showChoice"
        >&#xe6eb;</span
      >
      <div class="select" v-show="showChoice">
        <div
          class="select-item"
          v-for="item in titleSelect"
          :key="item.key"
          @click="handleSelect(item.key)"
        >
          {{ item.text }}
        </div>
      </div>
    </div>
    <div class="sellerchart" ref="trend_ref"></div>
  </div>
</template> 

<script>
export default {
  name: "Trend",
  data() {
    return {
      chartInstance: null,
      allData: "",
      showChoice: false,
      choiceType: "map",
      titleFontSize: 0,
    };
  },
  mounted() {
    this.initChart();
    this.getData();
    window.addEventListener("resize", this.screenAdapter);
    this.screenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
  },
  computed: {
    showTitle() {
      if (!this.allData) {
        return [];
      }
      return this.allData[this.choiceType].title;
    },
    titleSelect() {
      if (!this.allData) {
        return [];
      }
      return this.allData.type.filter((item) => {
        return item.key !== this.choiceType;
      });
    },
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
  },
  methods: {
    initChart() {
      this.chartInstance = this.$echart.init(this.$refs.trend_ref, "customed");

      const initOption = {
        title: {
          textStyle: {
            fontSize: 66,
          },
          left: 20,
          top: 20,
        },
        tooltip: {
          trigger: "axis",
        },
        legend: {
          left: "50%",
          top: "5%",
          icon: "circle",
        },

        grid: {
          top: "20%",
          left: "3%",
          right: "6%",
          bottom: "3%",
          containLabel: true,
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
      };
      initOption && this.chartInstance.setOption(initOption);
    },
    async getData() {
      const { data: ret } = await this.$http.get("trend");
      this.allData = ret;
      console.log(this.allData);
      this.updateChart();
    },
    updateChart() {
      const colorArr1 = [
        "rgba(11,168,44,0.5)",
        "rgba(44,110,255,0.5)",
        "rgba(22,242,217,0.5)",
        "rgba(254,33,30,0.5)",
        "rgba(250,105,0,0.5)",
      ];
      const colorArr2 = [
        "rgba(11,168,44,0)",
        "rgba(44,110,255,0)",
        "rgba(22,242,217,0)",
        "rgba(254,33,30,0)",
        "rgba(250,105,0,0)",
      ];
      const timerArr = this.allData.common.month;
      const valueArr = this.allData[this.choiceType].data;
      const seriesArr = valueArr.map((item, index) => {
        return {
          type: "line",
          data: item.data,
          name: item.name,
          stack: "this.choiceType",
          areaStyle: {
            color: new this.$echart.graphic.LinearGradient(0, 0, 0, 1, [
              {
                offset: 0,
                color: colorArr1[index],
              },
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      const legendArr = valueArr.map((item) => {
        return item.name;
      });
      const dataOption = {
        xAxis: {
          data: timerArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesArr,
      };
      dataOption && this.chartInstance.setOption(dataOption);
    },
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend_ref.offsetWidth / 200) * 3;
      const adapterOption = {
        title: {
          textStyle: {
            fontSize: this.titleFontSize,
          },
        },
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    handleSelect(currentType) {
      this.choiceType = currentType;
      this.updateChart();
      this.showChoice = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.title {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 10;
  color: #fff;
  .iconfont {
    margin-left: 7px;
    cursor: pointer;
  }
  .select {
    .select-item {
      cursor: pointer;
    }
  }
}
</style>